// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(sp.Skeleton)
    spine: sp.Skeleton = null;

    @property([cc.Slider])
    RGBSliders: cc.Slider[] = [];

    start() {

    }

    onSliderEvent(slider: cc.Slider) {
        let label = slider.node.getChildByName("value").getComponent(cc.Label);
        label.string = `${Math.round(slider.progress * 255)}`;
        this.updateMaterial();
    }

    private useCut: boolean = true;
    onClickChange() {
        this.useCut = !this.useCut;
        this.updateMaterial();
    }

    updateMaterial() {
        let spriteMaterial = this.icon.getMaterial(0);
        spriteMaterial.define("USE_CUT", this.useCut);
        let spineMaterial = this.spine.getMaterial(0);
        spineMaterial.define("USE_CUT", this.useCut);
        if (this.useCut) {
            spriteMaterial.setProperty("cutColor", new cc.Vec4(this.RGBSliders[0].progress, this.RGBSliders[1].progress, this.RGBSliders[2].progress, 0));
            spineMaterial.setProperty("cutColor", new cc.Vec4(this.RGBSliders[0].progress, this.RGBSliders[1].progress, this.RGBSliders[2].progress, 0));
        }
        if (CC_NATIVERENDERER)
            //@ts-ignore
            this.spine._updateMaterial();
    }
    // update (dt) {}
}
